﻿using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;
using MVCms.Core.DataAccess;
using MVCms.Core.Developers;

using NHibernate;
using System;

namespace MVCms.Core.Entities
{
    [Serializable]
    public class VirtualPage : BaseEntity
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VirtualPage"/> class.
        /// </summary>
        public VirtualPage()
        {
            this.Modules = new List<PageModule>();
            this.PageRoles = new List<PageRole>();
        }

        /// <summary>
        /// Gets or sets the parent page.
        /// </summary>
        /// <value>The parent.</value>
        public virtual VirtualPage Parent { get; set; }
        /// <summary>
        /// Gets or sets the page name.
        /// </summary>
        /// <value>The name.</value>
        public virtual string Name { get; set; }
        /// <summary>
        /// Gets the the urlfriendy name (!!NOT YET IMPLEMENTED!!)
        /// </summary>
        /// <value>The name of the URL friendly.</value>
        public virtual string UrlFriendlyName 
        { 
            get
            {
                return HttpUtility.UrlEncode(this.Name);
            }
        }
        /// <summary>
        /// Gets or sets the page title that will be displayed in the browser.
        /// </summary>
        /// <value>The title.</value>
        public virtual string Title { get; set; }
        /// <summary>
        /// Gets or sets the list of all modules present on that page
        /// </summary>
        /// <value>The modules.</value>
        public virtual IList<PageModule> Modules { get; set; }
        /// <summary>
        /// Gets or sets the roles assigned to the page
        /// </summary>
        /// <value>The page roles.</value>
        public virtual IList<PageRole> PageRoles { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this page must be visible in a menu structure
        /// </summary>
        /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
        public virtual bool Visible { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this page should be clickable in a menu structure
        /// </summary>
        /// <value><c>true</c> if [enable hyperlink]; otherwise, <c>false</c>.</value>
        public virtual bool EnableHyperlink { get; set; }
        /// <summary>
        /// Gets the URL to the page.
        /// </summary>
        /// <value>The URL.</value>
        [McContext.IgnoreAttribute]
        public virtual string Url
        {
            get
            {
                VirtualPage page = this;
                string url = "/" + page.Name;

                while (page.Parent != null)
                {
                    url = string.Format("{0}{1}", "/" + page.Parent.Name, url);
                    page = page.Parent;
                }

                return url;
            }
        }
        /// <summary>
        /// Gets or sets a value indicating whether the page can be displayed to the currently logged in user
        /// </summary>
        /// <value><c>true</c> if display; otherwise, <c>false</c>.</value>
        [McContext.IgnoreAttribute]
        public virtual bool Display { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether the page is in edir mode.
        /// </summary>
        /// <value><c>true</c> if [edit mode]; otherwise, <c>false</c>.</value>
        [McContext.IgnoreAttribute]
        public virtual bool EditMode { get; set; }
    }
}